﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Bzzb.Model;
using FreeSql.Internal.Model;
using Magicodes.ExporterAndImporter.Core;
using Magicodes.ExporterAndImporter.Core.Extension;
using Magicodes.ExporterAndImporter.Excel;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using OfficeOpenXml;

namespace Bzzb.Controllers
{
    public class WeiZhiXinXiGuanLiController : BaseController
    {
        private readonly IWebHostEnvironment _env;
        private readonly IConfiguration _configuration;
        private readonly ILogger<WeiZhiXinXiGuanLiController> _logger;
        public WeiZhiXinXiGuanLiController(ILogger<WeiZhiXinXiGuanLiController> logger, IFreeSql fsql, IConfiguration configuration, IWebHostEnvironment env) : base(fsql)
        {
            this._env = env;
            this._logger = logger;
            this._configuration = configuration;
        }

        [HttpPost]
        public async Task<IDictionary<string, Object>> Index([FromForm] string content, [FromForm] int doPage = 1, [FromForm] int pageIndex = 1, [FromForm] int pageSize = 10)
        {
            DynamicFilterInfo dyfilter = JsonConvert.DeserializeObject<DynamicFilterInfo>(@"
{
  'Logic' : 'And',
  'Filters' :
  [
    {
      'Field' : 'name',
      'Operator' : 'Contains',
      'Value' : '" + content + @"',
    }
  ]
}
");
            List<WeiZhiXinXiGuanLi> weizhixinxiguanlis;
            long total = 0;
            if (doPage == 0)
            {
                weizhixinxiguanlis = await _fsql.Select<WeiZhiXinXiGuanLi>().WhereDynamicFilter(dyfilter).ToListAsync();
            }
            else {
                weizhixinxiguanlis = await _fsql.Select<WeiZhiXinXiGuanLi>().WhereDynamicFilter(dyfilter).Count(out total).Page(pageIndex, pageSize).ToListAsync();
            }
            
            _logger.LogInformation(_fsql.Select<WeiZhiXinXiGuanLi>().WhereDynamicFilter(dyfilter).Page(pageIndex, pageSize).ToSql());
            JObject data = new();
            data.Add("total", total);
            data.Add("list", JArray.FromObject(weizhixinxiguanlis));
            return RenderJson(data: data);
        }

        /// <summary>
        /// 获取所有未使用的工具
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
//        [HttpPost]
//        public async Task<IDictionary<string, Object>> GetAllUnusedWeiZhiXinXiGuanLis([FromForm] string content)
//        {
//            DynamicFilterInfo dyfilter = JsonConvert.DeserializeObject<DynamicFilterInfo>(@"
//{
//  'Logic' : 'And',
//  'Filters' :
//  [
//    {
//      'Field' : 'name',
//      'Operator' : 'Contains',
//      'Value' : '" + content + @"',
//    }
//  ]
//}
//");
//            List<WeiZhiXinXiGuanLi> weizhixinxiguanlis = await _fsql.Select<WeiZhiXinXiGuanLi>().Where(t => t.IsUsed == 0).WhereDynamicFilter(dyfilter).ToListAsync();
//            _logger.LogInformation(_fsql.Select<WeiZhiXinXiGuanLi>().Where(t => t.IsUsed == 0).WhereDynamicFilter(dyfilter).ToSql());
//            return RenderJson(data: weizhixinxiguanlis);
//        }

        [HttpPost]
        public async Task<IDictionary<string, Object>> Save([FromForm] WeiZhiXinXiGuanLi weizhixinxiguanli)
        {
            long id = await _fsql.Insert(weizhixinxiguanli).ExecuteIdentityAsync();
            weizhixinxiguanli.Id = id;

            return RenderJson(data: weizhixinxiguanli);
        }

        [HttpPost]
        public async Task<IDictionary<string, Object>> Delete([FromForm] long id) {
            int affrows = await _fsql.Delete<WeiZhiXinXiGuanLi>().Where(t => t.Id == id).ExecuteAffrowsAsync();
            return RenderJson(data: affrows);
        }

        [HttpPost]
        public async Task<IDictionary<string, Object>> Update([FromForm] WeiZhiXinXiGuanLi weizhixinxiguanli) {
            int affrows = await _fsql.Update<WeiZhiXinXiGuanLi>().SetSourceIgnore(weizhixinxiguanli, colval => colval == null).ExecuteAffrowsAsync();
            return RenderJson(data: affrows);
        }

        [HttpPost]
        public async Task<IDictionary<string, Object>> Show([FromForm] long id) {
            WeiZhiXinXiGuanLi weizhixinxiguanli = await _fsql.Select<WeiZhiXinXiGuanLi>().Where(t => t.Id == id).ToOneAsync();
            return RenderJson(data: weizhixinxiguanli);
        }
    }
}
